<template>
  <Dialog v-model="dialogVisible" title="选择测试策略" width="1000">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="85px"
      >
        <el-form-item label="测试策略" prop="strategyName">
          <ns-input
            v-model="queryParams.strategyName"
            class="!w-240px"
            clearable
            placeholder="请输入测试策略"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list" row-key="id" border ref="treeTableRef">
        <el-table-column width="50" label="" align="center">
          <template #default="{ row }">
            <el-checkbox v-model="row.select" @change="selectChange($event, row)" />
          </template>
        </el-table-column>
        <el-table-column type="index" width="80" label="序号" align="center" />
        <el-table-column prop="strategyName" label="测试策略" align="center" />
        <el-table-column prop="strategyDesc" label="描述" align="center" />
        <el-table-column prop="createTime" label="创建时间" align="center" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.page"
        :total="total"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </template>
  </Dialog>
</template>
<script setup>
import { strategyPage } from '@/api/strategyManage/strategy/index'
const dialogVisible = defineModel('visible', { type: Boolean })
const props = defineProps({
  type: {
    type: String,
    default: 'create'
  }
})
const message = useMessage() // 消息弹窗
const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
let selectInfo = [] // 选中的数据

const queryParams = reactive({
  page: 1,
  pageSize: 10,
  strategyName: '',
  type: 'secondary'
})
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 选中取消 */
const selectChange = (e, row) => {
  // 新增时可多选
  if (props.type == 'create') {
    if (e) {
      selectInfo.push(row)
    } else {
      selectInfo = selectInfo.filter((item) => item.id != row.id)
    }
  } else {
    // 更新时单选
    if (e) {
      selectInfo = [row]
    } else {
      selectInfo = selectInfo.filter((item) => item.id != row.id)
    }
    list.value.forEach((item) => {
      item.select = false
      if (selectInfo.find((eve) => eve.id == item.id)) {
        item.select = true
      }
    })
  }
}

const treeTableRef = ref()
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { count, data } = await strategyPage(toRaw(queryParams))
    data.forEach((item) => {
      item.select = false
      if (selectInfo.find((eve) => eve.id == item.id)) {
        item.select = true
      }
    })
    list.value = data
    total.value = count
  } finally {
    loading.value = false
  }
}
getList()

const emit = defineEmits(['success'])
/** 提交表单 */
const handleSubmit = async (e) => {
  e.preventDefault()

  if (selectInfo.length == 0) {
    message.warning('请选择方案')
    return
  }
  dialogVisible.value = false
  console.log('selectInfo', selectInfo)
  // 发送操作成功的事件
  emit('success', selectInfo)
}
</script>
